package com.jkoss.wine.product.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.product.entity.MerchantProductType;
import com.jkoss.wine.product.entity.Product;
import com.jkoss.wine.product.service.IMerchantProductTypeService;
import com.jkoss.wine.product.service.IProductService;

/**
 * 商品分类 前端控制器
 * 
 * @Author nihao
 * @Version 1.0, 2019-08-12
 * @See
 * @Since com.ossjk.oa.product.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/product/product")
public class MerchantProductTypeController extends BaseController {


	@Autowired
	private IMerchantProductTypeService iMerchantProductTypeService;
	
	@Autowired
	private IProductService iProductService;
	
	@RequestMapping("/list")
	@RequiresPermissions("/product/product/list")
	public String list(DwzPageBean dwzPageBean,String productTypeName, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		dwzPageBean.getCountResultMap().put("productTypeName", productTypeName);
		Wrapper wrapper = Condition.create().like("productTypeName", productTypeName);
		Page resultPage = iMerchantProductTypeService.selectPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		}
		
		return "product/product/list";
	}

	@RequestMapping("/toInsert")
	@RequiresPermissions("/product/product/toInsert")
	public String toInsert(HttpServletRequest request, ModelMap map , HttpServletResponse response) {
		
		return "product/product/edit";
	}

	@RequestMapping("/insert")	
	@RequiresPermissions("/product/product/toInsert")
	@ResponseBody
	public Object insert(@Valid MerchantProductType merchant_product_type, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// 查看demo代码，工具类
		// 添加时间
		merchant_product_type.setAddTime(CommonUtil.getDate());
		// 操作人:正在登录系统的人id
		merchant_product_type.setOperator("123");
		// 是否删除 0否1是
		merchant_product_type.setDel(0);
		
		if (iMerchantProductTypeService.insertMerchantProductType(merchant_product_type)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

//		http://127.0.0.1:8091/wineproduct/product/list
	}

	@RequestMapping("/toUpdate")
	@RequiresPermissions("/product/product/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		
		MerchantProductType merchantProductType = iMerchantProductTypeService.selectById(id);
		
		Wrapper wrapper = Condition.create().ne("del", 1); 	
		List list = iProductService.selectList(wrapper);
		map.put("product",list);
		
		map.put("record", merchantProductType);
		return "product/product/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/product/product/toUpdate")
	@ResponseBody
	public Object update(@Valid MerchantProductType merchantProductTypeId, HttpServletRequest request, HttpServletResponse response) {
		if (iMerchantProductTypeService.updateById(merchantProductTypeId)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	@RequestMapping("/delete")
	@RequiresPermissions("/product/product/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		if (iMerchantProductTypeService.deleteBatchIds(Arrays.asList(id))) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}
	

}

